package com.admin.client;

import com.admin.client.locale.AdminConstants;
import com.admin.client.locale.AdminMessages;
import com.admin.client.mvc.controllers.AppController;
import com.admin.client.mvc.controllers.UsuarioController;
import com.admin.client.mvc.events.AppEvents;
import com.admin.share.model.UsuarioDTO;
import com.admin.share.services.LoginService;
import com.admin.share.services.LoginServiceAsync;
import com.admin.share.services.MenuService;
import com.admin.share.services.UsuarioService;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;



public class Admin implements EntryPoint {
	
	
	public void onModuleLoad() {
		Registry.register(AppConstants.LOGIN_SERVICE, GWT.create(LoginService.class));
		Registry.register(AppConstants.MENU_SERVICE, GWT.create(MenuService.class));
		Registry.register(AppConstants.USUARIO_SERVICE, GWT.create(UsuarioService.class));
		
		Registry.register(AppConstants.CONSTANTS, GWT.create(AdminConstants.class));
		Registry.register(AppConstants.MESSAGES, GWT.create(AdminMessages.class));
		
		final Dispatcher dispatcher = Dispatcher.get();
		dispatcher.addController(new AppController());
		dispatcher.addController(new UsuarioController());		
					
		
		AsyncCallback<UsuarioDTO> callback = new AsyncCallback<UsuarioDTO>() {
			
			@Override
			public void onSuccess(UsuarioDTO result) {
				if(result != null){					
					dispatcher.dispatch(AppEvents.Init, result);					
				}else{					
					dispatcher.dispatch(AppEvents.Auth);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				dispatcher.dispatch(AppEvents.Error);				
			}
		};
		final LoginServiceAsync ls = Registry.get(AppConstants.LOGIN_SERVICE);
		ls.getUsuarioSession(callback);
		
//		Window.setTitle(constants.administradorConsultoriaSTI());
//		Usuarios.admin = this;
//		
//		GXT.hideLoadingPanel("loading");
//		
//		loadLogin();
	}
	
//	public void loadLogin() {
//	    LoginDialog dialog = new LoginDialog();
//	    dialog.setClosable(false);
//	    
//	    dialog.show();
//	}
//
//	public void dolayout() {
//		BorderLayout bl = new BorderLayout();
//		viewport = new Viewport();
//		viewport.setLayout(bl);
//		
//		north = new LayoutContainer();
//		
//		ContentPanel west = new ContentPanel();
//		west.setHeading(constants.menu());
//		
//		
//	}
//
//	public void addButtonExit(String fullname) {
//		// TODO Auto-generated method stub
//		
//	}
}